<template>
  <div class="login">
    <div class="denglu">
      <!-- 头像 -->
      <div class="touxiang">
        <img src="../assets/logo.png" />
      </div>
      <!-- 表单 -->
      <el-form
        ref="loginfromref"
        class="form"
        :rules="loginrules"
        :model="loginfrom"
        label-width="0px"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginfrom.username"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginfrom.password"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <!-- 登录,重置 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="denglu">登录</el-button>
          <el-button type="info" @click="chongzhi">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginfrom: {
        username: "admin",
        password: "123456",
      },
      loginrules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    chongzhi() {
      /*  resetFields是elementui提供的一个重置方法 */
      this.$refs.loginfromref.resetFields();
    },
    denglu() {
      /* validate是elementui提供的一个验证提交请求
            包含箭头函数第一个值是校验结果
            { data: res }是拿到获取对象中的data
            async是吧请求变成异步简化数据
            一个函数如果加上async，那么该函数就会返回一个Promise
            async就是将函数返回值使用Promise.resolve()包裹了下
            await配合async使用
            await将异步代码改造成了同步代码
        */
      this.$refs.loginfromref.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginfrom);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
        this.$message.success(res.meta.msg);
        /*  console.log(res); */
       /*  console.log(window.sessionStorage); */
        /* sessionStorage储存是在浏览器关闭销毁 */
        window.sessionStorage.setItem("token", res.data.token);
       /*  console.log(window.sessionStorage); */
        /* 路由跳转 */
        /* console.log(this.$router); */
        this.$router.push("/Home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: #264b6b;
  height: 100%;
  .denglu {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .touxiang {
      width: 130px;
      height: 130px;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}
.form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>